package com.handinfo.android.ui.window;

import java.io.DataInputStream;
import java.io.IOException;

import android.graphics.Bitmap;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.core.graphics.DWFont;
import com.handinfo.android.game.item.Item;
import com.handinfo.android.ui.IUIWindows;
import com.handinfo.android.ui.UIWindows;
import com.handinfo.android.uicontrols.DWControlTools;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.uicontrols.DWListener;
import com.handinfo.android.uicontrols.controls.DWBackground;
import com.handinfo.android.uicontrols.controls.DWButton;
import com.handinfo.android.uicontrols.controls.DWFrame;
import com.handinfo.android.uicontrols.controls.DWImageBox;
import com.handinfo.android.uicontrols.controls.DWInputBox;
import com.handinfo.android.uicontrols.controls.DWLabel;
import com.handinfo.android.uicontrols.controls.DWMessageBox;
import com.handinfo.android.uicontrols.controls.DWTextbox;
import com.handinfo.android.uicontrols.controls.DWTitle;
import com.handinfo.android.utils.Tools;

public class UIWarrantWindow implements IUIWindows
{
	private Bitmap img_close;
	private Bitmap img_button;
	private Bitmap img_anxia;
	private Bitmap img_beforePage;
	private Bitmap img_nextPage;
	private Bitmap img_background;
	private Bitmap img_yuanbao;
	private Bitmap img_qian;
	
	private long m_roleID=0;
	private String m_roleName;
	public  long m_money=0;
	public  long m_yuanbao=0;
	private String m_msg;
	private byte m_type=0;
	
	public void init()
	{
		img_close=UIWindows.createImage("/img/newui/fanhuianniu_1.gnp");
		img_button=UIWindows.createImage("/img/newui/anniu_1.gnp");
		img_anxia=UIWindows.createImage("/img/newui/anniu_1ax.gnp");
		img_beforePage = UIWindows.createImage("/img/newui/haoyoufyan_2.gnp");
		img_nextPage = UIWindows.createImage("/img/newui/haoyoufyan_1.gnp");
		img_background=UIWindows.createImage("/img/newui/beibaobj_1.gnp");
		
		img_yuanbao=UIWindows.createImage("/img/newui/yuanbao_1.gnp");
		img_qian=UIWindows.createImage("/img/newui/jinbi_1.gnp");
	}

	private DWFrame m_Frame;
	private DWTitle m_title;
	private DWImageBox m_closeImgBox;
	private DWBackground m_backgound;
	private DWButton m_backButton;
	private DWButton m_okButton;
	private DWTextbox m_msgTextBox;
	private DWLabel m_msgLabel;
	private DWImageBox m_beforImg;
	private DWImageBox m_nextImg;
	private DWInputBox m_awardMoney;	
	private DWInputBox m_awardYuanbao;
	private DWLabel m_moneyType;
	private DWLabel m_yuanbaoType;
	private DWTextbox m_tipsTextbox;
	private DWInputBox m_msgInput;
	private DWTextbox m_feeTexbox;
	
	private DWImageBox m_qianimage;
	private DWImageBox m_yuanbaoimage;
	
	private void initFrame()
	{
		if(DWControlsManager.getInstance().contains(m_Frame))
		{
			DWControlsManager.getInstance().removeControl(m_Frame);
			m_Frame=null;
		}
			
		m_Frame=new DWFrame(DWFrame.FRAME_TYPE_HALF_HCENTER);
		m_Frame.setClickClose(false);
		
		m_title=new DWTitle("通缉令", m_Frame);
		m_Frame.addControl(m_title);
		
		m_closeImgBox=new DWImageBox(img_close);
		m_closeImgBox.addListener(new DWListener() {			
			public void OnDoubleClick()
			{
			}
			
			public void OnClick()
			{
				close((byte) 0);
			}
		});
		m_closeImgBox.setNearAnchor(m_title, Tools.RIGHT_VCENTER, Tools.RIGHT_VCENTER);
		m_Frame.addControl(m_closeImgBox);
		
		m_backgound=new DWBackground(img_background,m_Frame.getShowWidth()-40, m_Frame.getShowHeight()-img_button.getHeight()-50);
		m_backgound.setNearAnchor(m_Frame, Tools.LEFT_TOP, Tools.LEFT_TOP, 20, 20);
		m_Frame.addControl(m_backgound);
		
		m_backButton=new DWButton("取消", img_button);
		m_backButton.setDownImage(img_anxia);
		m_backButton.setNearAnchor(m_backgound, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 20, 10);
		m_backButton.addListener(new DWListener() {			
			public void OnDoubleClick()
			{
			}
			
			public void OnClick()
			{
				close((byte) 0);
			}
		});
		m_Frame.addControl(m_backButton);
		
		
		
//		m_msgTextBox=new DWTextbox("",m_backgound.getShowWidth()-40,(m_backgound.getShowHeight()-40)/3);
		m_msgTextBox=new DWTextbox(" ` ` ",m_backgound.getShowWidth()-40);
		m_msgTextBox.setNearAnchor(m_backgound, Tools.HCENTER_TOP, Tools.HCENTER_TOP, 0, 10);
		m_Frame.addControl(m_msgTextBox);
				
		m_msgLabel=new DWLabel("奖金数额：");
		m_msgLabel.setBackgroundColor(Tools.ALPHA);
		
		int h=0;
		int w=0;
		if(DWGameManager.AdpaterType == DWGameManager.AD_HIGH){
			h=40;
			w=170;
		}else{
			w=120;
			h=30;
		}
		
		
		m_awardMoney=new DWInputBox(w, h, m_Frame);
		m_awardYuanbao=new DWInputBox(w, h, m_Frame);
		m_feeTexbox=new DWTextbox("手续费："+  DWControlTools.CHANGE_ROW +
				"<mark c=FFC004>"+"金币通缉目标被击杀"+"</mark>"+  DWControlTools.CHANGE_ROW +
				"<mark c=FFC004>"+"将消失5%金币，虚弱30分钟"+"</mark>"+  DWControlTools.CHANGE_ROW +
				"<mark c=FFC004>"+"元宝通缉目标被击杀"+"</mark>"+  DWControlTools.CHANGE_ROW +
				"<mark c=FFC004>"+"将消失20%金币，虚弱2小时"+"</mark>");
		
		m_awardMoney.setText("0");
		m_awardMoney.setNearAnchor(m_msgTextBox, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, m_msgLabel.getShowWidth(), 10);
		m_awardMoney.addListener(new DWListener() {			
			public void OnDoubleClick()
			{
				
			}
			
			public void OnClick()
			{
				if (UINpcShopWindow.isNumber(m_awardMoney.getText()))
				{
					int num = Integer.valueOf(m_awardMoney.getText());
					if(num<0)
					{
						DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "请输入正确的数字！");
						num = 0;
						m_money=num;
						m_awardMoney.setText("0");
						m_feeTexbox.setText("手续费："+  DWControlTools.CHANGE_ROW +
								"<mark c=FFC004>"+"金币通缉目标被击杀"+"</mark>"+  DWControlTools.CHANGE_ROW +
								"<mark c=FFC004>"+"将消失5%金币，虚弱30分钟"+"</mark>"+  DWControlTools.CHANGE_ROW +
								"<mark c=FFC004>"+"元宝通缉目标被击杀"+"</mark>"+  DWControlTools.CHANGE_ROW +
								"<mark c=FFC004>"+"将消失20%金币，虚弱2小时"+"</mark>");
					}else if(num<50000){
						DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "您输入的金钱数过低！最少5万金钱");
						num = 0;
						m_money=num;
						m_awardMoney.setText("0");
						m_feeTexbox.setText("手续费："+  DWControlTools.CHANGE_ROW +
								"<mark c=FFC004>"+"金币通缉目标被击杀"+"</mark>"+  DWControlTools.CHANGE_ROW +
								"<mark c=FFC004>"+"将消失5%金币，虚弱30分钟"+"</mark>"+  DWControlTools.CHANGE_ROW +
								"<mark c=FFC004>"+"元宝通缉目标被击杀"+"</mark>"+  DWControlTools.CHANGE_ROW +
								"<mark c=FFC004>"+"将消失20%金币，虚弱2小时"+"</mark>");
					}
					else if(num>DWGameManager.getInstance().m_role.m_gold)
					{
						DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "您输入的数目，大于当前拥有金钱数！");
						num = 0;
						m_money=num;
						m_awardMoney.setText("0");
						m_feeTexbox.setText("手续费："+  DWControlTools.CHANGE_ROW +
								"<mark c=FFC004>"+"金币通缉目标被击杀"+"</mark>"+  DWControlTools.CHANGE_ROW +
								"<mark c=FFC004>"+"将消失5%金币，虚弱30分钟"+"</mark>"+  DWControlTools.CHANGE_ROW +
								"<mark c=FFC004>"+"元宝通缉目标被击杀"+"</mark>"+  DWControlTools.CHANGE_ROW +
								"<mark c=FFC004>"+"将消失20%金币，虚弱2小时"+"</mark>");
					}else
					{
						if(num>=100000000){
							num=99999999;
						}
						m_feeTexbox.setText("手续费：20%金币"+  DWControlTools.CHANGE_ROW +
								"<mark c=FFC004>"+"金币通缉目标被击杀"+"</mark>"+  DWControlTools.CHANGE_ROW +
								"<mark c=FFC004>"+"将消失5%金币，虚弱30分钟"+"</mark>"+  DWControlTools.CHANGE_ROW +
								"<mark c=FFC004>"+"元宝通缉目标被击杀"+"</mark>"+  DWControlTools.CHANGE_ROW +
								"<mark c=FFC004>"+"将消失20%金币，虚弱2小时"+"</mark>");
						m_money=num;
						m_yuanbao=0;
						m_type = 0;
						m_awardYuanbao.setText("0");
						Tools.debugPrintln("m_money:"+m_money+"m_yuanbao"+m_yuanbao);
					}
					
						
				
				}				
				m_awardMoney.setText(""+m_money);					
			}
		});
		m_Frame.addControl(m_awardMoney);
		
		m_msgLabel.setNearAnchor(m_awardMoney, Tools.RIGHT_VCENTER, Tools.LEFT_VCENTER);
		m_Frame.addControl(m_msgLabel);
		
//		m_moneyType=new DWLabel("金");
//		m_moneyType.setBackgroundColor(Tools.ALPHA);
//		m_moneyType.setNearAnchor(m_awardMoney, Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER,5,0);
//		m_Frame.addControl(m_moneyType);
		int x=0;
		if(DWGameManager.AdpaterType == DWGameManager.AD_HIGH){
			x=15;
		}else{
			x=5;
		}
		m_qianimage=new DWImageBox(img_qian);
		m_qianimage.setNearAnchor(m_awardMoney, Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER,x,0);
		m_Frame.addControl(m_qianimage);
		
		
		
//		m_tipsTextbox=new DWTextbox("通缉榜文：（30字）");
//		m_tipsTextbox.setNearAnchor(m_msgTextBox, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 0, 50);		
//		m_Frame.addControl(m_tipsTextbox);
//		
//		m_msgInput=new DWInputBox(m_backgound.getShowWidth()-40, m_backgound.getShowHeight()/3, m_Frame);
//		m_msgInput.setNearAnchor(m_tipsTextbox, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 0, 10);
//		m_msgInput.addListener(new DWListener() {			
//			public void OnDoubleClick()
//			{				
//			}
//			
//			public void OnClick()
//			{
//				String msg=m_msgInput.getText();
//				if(msg.length()>30)
//				{
//					msg=msg.substring(0, 30);
//					DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "输入已超过上限");
//				}
//				m_msg=msg;
//				m_msgInput.setText(msg);
//			}
//		});
//		m_Frame.addControl(m_msgInput);
		
		
		int y = 0;
		if(DWGameManager.AdpaterType == DWGameManager.AD_HIGH){
			y=60;
		}else{
			y=50;
		}
		
		m_awardYuanbao.setText("0");
		m_awardYuanbao.setNearAnchor(m_msgTextBox, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, m_msgLabel.getShowWidth(), y);
		m_awardYuanbao.addListener(new DWListener() {			
			public void OnDoubleClick()
			{
			}
			
			public void OnClick()
			{
				if (UINpcShopWindow.isNumber(m_awardYuanbao.getText()))
				{
					int num = Integer.valueOf(m_awardYuanbao.getText());
					if(num<0)
					{
						DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "请输入正确的数字！");
						num = 0;
						m_yuanbao=num;
						m_awardYuanbao.setText("0");
						m_feeTexbox.setText("手续费："+  DWControlTools.CHANGE_ROW +
								"<mark c=FFC004>"+"金币通缉目标被击杀"+"</mark>"+  DWControlTools.CHANGE_ROW +
								"<mark c=FFC004>"+"将消失5%金币，虚弱30分钟"+"</mark>"+  DWControlTools.CHANGE_ROW +
								"<mark c=FFC004>"+"元宝通缉目标被击杀"+"</mark>"+  DWControlTools.CHANGE_ROW +
								"<mark c=FFC004>"+"将消失20%金币，虚弱2小时"+"</mark>");
					}else if(num<20){
						DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "请输入的元宝数量过低！最少20元宝");
						num = 0;
						m_yuanbao=num;
						m_awardYuanbao.setText("0");
						m_feeTexbox.setText("手续费："+  DWControlTools.CHANGE_ROW +
								"<mark c=FFC004>"+"金币通缉目标被击杀"+"</mark>"+  DWControlTools.CHANGE_ROW +
								"<mark c=FFC004>"+"将消失5%金币，虚弱30分钟"+"</mark>"+  DWControlTools.CHANGE_ROW +
								"<mark c=FFC004>"+"元宝通缉目标被击杀"+"</mark>"+  DWControlTools.CHANGE_ROW +
								"<mark c=FFC004>"+"将消失20%金币，虚弱2小时"+"</mark>");
					}
					else if(num>DWGameManager.getInstance().m_role.m_rmb)
					{
						DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "您输入的数目，大于当前拥有元宝数！");
						num = 0;
						m_yuanbao=num;
						m_awardYuanbao.setText("0");
						m_feeTexbox.setText("手续费："+  DWControlTools.CHANGE_ROW +
								"<mark c=FFC004>"+"金币通缉目标被击杀"+"</mark>"+  DWControlTools.CHANGE_ROW +
								"<mark c=FFC004>"+"将消失5%金币，虚弱30分钟"+"</mark>"+  DWControlTools.CHANGE_ROW +
								"<mark c=FFC004>"+"元宝通缉目标被击杀"+"</mark>"+  DWControlTools.CHANGE_ROW +
								"<mark c=FFC004>"+"将消失20%金币，虚弱2小时"+"</mark>");
					}else
					{
						if(num>=100000000){
							num=99999999;
						}
						m_feeTexbox.setText("手续费：50%元宝"+  DWControlTools.CHANGE_ROW +
								"<mark c=FFC004>"+"金币通缉目标被击杀"+"</mark>"+  DWControlTools.CHANGE_ROW +
								"<mark c=FFC004>"+"将消失5%金币，虚弱30分钟"+"</mark>"+  DWControlTools.CHANGE_ROW +
								"<mark c=FFC004>"+"元宝通缉目标被击杀"+"</mark>"+  DWControlTools.CHANGE_ROW +
								"<mark c=FFC004>"+"将消失20%金币，虚弱2小时"+"</mark>");
						m_yuanbao=num;
						m_money=0;
						m_type = 1;
						m_awardMoney.setText("0");
						Tools.debugPrintln("m_money:"+m_money+"m_yuanbao"+m_yuanbao);
					}					
				}				
				m_awardYuanbao.setText(""+m_yuanbao);					
			}
		});
		m_Frame.addControl(m_awardYuanbao);
		
		
//		m_yuanbaoType=new DWLabel("元宝");
//		m_yuanbaoType.setBackgroundColor(Tools.ALPHA);
//		m_yuanbaoType.setNearAnchor(m_awardYuanbao, Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER,5,0);
//		m_Frame.addControl(m_yuanbaoType);
		
		m_yuanbaoimage=new DWImageBox(img_yuanbao);
		m_yuanbaoimage.setNearAnchor(m_awardYuanbao, Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER,x,0);
		m_Frame.addControl(m_yuanbaoimage);
		
		
		
		

		
		
		
		int y1=0;
		if(DWGameManager.AdpaterType == DWGameManager.AD_HIGH){
			m_feeTexbox.setFont(DWFont.getFont(30));
			y1 = 70;
		}else{
			y1 = 50;
			m_feeTexbox.setFont(DWFont.getFont(18));
		}
		m_feeTexbox.setNearAnchor(m_msgLabel, Tools.LEFT_TOP, Tools.LEFT_BOTTOM,0,y1);
		m_Frame.addControl(m_feeTexbox);	
		
		
		
		m_okButton=new DWButton("确定", img_button);
		m_okButton.setDownImage(img_anxia);
		m_okButton.setNearAnchor(m_backgound, Tools.RIGHT_TOP, Tools.RIGHT_BOTTOM, -20, 10);
		m_okButton.addListener(new DWListener() {			
			public void OnDoubleClick()
			{
			}
			
			public void OnClick()
			{
				if(m_money<50000&&m_yuanbao<20)
				{
					DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "请输入赏金");
				}else
				{
					close((byte) 0);
					if(m_money>0){
						DWGameManager.getInstance().getSendMessage().sendEnemyWanted(m_roleID, m_roleName, m_type, m_money);
					}else if(m_yuanbao>0){
						DWGameManager.getInstance().getSendMessage().sendEnemyWanted(m_roleID, m_roleName, m_type, m_yuanbao);
					}
					m_money = 0;
					m_yuanbao = 0;
					m_awardMoney.setText("0");
					m_awardYuanbao.setText("0");
				}
			}
		});
		m_Frame.addControl(m_okButton);
		
	}
	
	public void  setMessage(long roleID,String roleName)
	{
		m_roleID=roleID;
		m_roleName=roleName;
		
		String msg="通缉目标："+"`击杀时限：48小时";
		m_msgTextBox.setText(msg);
	}
	
	public void open(byte index)
	{
		initFrame();
		DWControlsManager.getInstance().addControl(m_Frame);
	}

	public void close(byte index)
	{
		DWControlsManager.getInstance().removeControl(m_Frame);		
		m_Frame=null;
//		m_money = 0;
//		m_yuanbao = 0;
//		m_awardMoney.setText("0");
//		m_awardYuanbao.setText("0");
	}

	public void dispose()
	{
	}

	public void logic()
	{
	}
	
	/**
	 * 发布通缉令结果
	 * */
	public void recvSeeItemInfos(DataInputStream dis) throws IOException
	{
		try {
			short state=dis.readShort();
			switch (state) {
				case 0:
					DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "通缉令发布失败");
					break;
				case 1:
					DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "通缉令发布成功");
					break;
			}
			
			String msg=dis.readUTF();
			
			DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_A, msg);
		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}
	/**
	 * 商城购买通缉令选项
	 */
	public void shoptongji(){
		final DWMessageBox socialmb = new DWMessageBox("提示", "元宝通缉需要《通缉令牌》道具,请问您是否购买？","购买","取消");
		socialmb.addListener(new DWListener() {
			public void OnDoubleClick()
			{
			}

			public void OnClick()
			{
				if (socialmb.getResult() == DWMessageBox.SOFTKEY_OK) {
					DWGameManager.getInstance().getSendMessage().sendOpenVipShop((byte) 1, (byte) 0);
					DWControlsManager.getInstance().removeControl(socialmb);
				}
				else if (socialmb.getResult() == DWMessageBox.SOFTKEY_BACK) {
					DWControlsManager.getInstance().removeControl(socialmb);
				}
			}
		});
		DWControlsManager.getInstance().addControl(socialmb);
	}
}
